<?php
/**
 * MPE个人中心 -- 收货地址
 * @author LiuYi
 * @name AddressInfoAction
 * @Create 2016-11-1 14:46:35
 * @Copyright MPE.Fiidee.Monda.Cn
 */
namespace member\action;
use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;

class AddressInfoAction extends CommonAction {

    protected $serviceBean="member.receiptAddress.service";
    protected $member_id;

    public function __construct()
    {
        parent::__construct();
        $this->member_id = $this->_user->__get('id');
    }

    /**
     * 收货地址列表页面
     * @param HttpRequest $request
     */
    public function index(HttpRequest $request)
    {
        $this -> setConditions(array('member_id'=>$this->member_id,'add_type'=>0));
        $service = Beans::get($this->getServiceBean());
        $total = $service->count($this->getConditions());
        //parent::index($request);
        $items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
            array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
        $this->assign('items', $items);
        $this->assign('total', $total);
        $this -> setView('person/person_address');
    }


    /**
     * 添加地址
     * @param HttpRequest $request
     */
    public function add(HttpRequest $request) {
        $this->assign("add",true);
        $this-> index($request);
    }

    /**
     * 添加地址实际操作，数据发送到此处
     * @param HttpRequest $request
     */
    public function do_add(HttpRequest $request)
    {
        $this -> setConditions(array('member_id'=>$this->member_id,'add_type'=>0));
        $service = Beans::get($this->getServiceBean());
        $total = $service->count($this->getConditions());
        if($total >= 10){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"你已经添加了10条收货地址了",null);
        }
        $data = $request ->getParameter('data');
        $data['prov'] = $request -> getParameter('province');
        $data['city'] = $request -> getParameter('city');
        $data['dist'] = $request -> getParameter('dist');
        $data['detail_address'] = $request -> getParameter('detail').$data['address'];
        $data['member_id'] = $this -> member_id;
        $data['add_type'] = 0;//用户主动添加的

        //如果是默认地址的话，就干掉原来的默认地址
        if( $data['default_type'] == 1 ){
            $this -> setNotDefault($this -> member_id);
        }
        return parent::insert($data); // TODO: Change the autogenerated stub
    }

    /**
     * 编辑地址页
     * @param HttpRequest $request
     */
    public function edit(HttpRequest $request)
    {
        $conditions['member_id'] = $this -> member_id;
        $this -> assign('edit',true);
        $this -> assign('conditions',$conditions);
        parent::edit($request);
        $this -> index($request);
        $this -> setView('person/person_address');
    }
    /**
     * 编辑地址 ajax
     * @param HttpRequest $request
     */
    public function editAjax(HttpRequest $request)
    {
        $conditions['id'] = $request->getParameter('id');
        $conditions['member_id'] = $this -> member_id;
        parent::edit($request);
        $service = Beans::get($this->getServiceBean());
        //parent::index($request);
        $item = $service->getItem($conditions);

        if(empty($item)){

            AjaxResult::ajaxFailtureResult();
        }else{
            AjaxResult::ajaxResult('000','处理成功',$item);
        }

    }

    /**
     * 编辑实际操作动作，数据发送到此处
     * @param HttpRequest $request
     */
    public function do_edit(HttpRequest $request)
    {
        $data = $request -> getParameter('data');
        $data['prov'] = $request -> getParameter('province');
        $data['city'] = $request -> getParameter('city');
        $data['dist'] = $request -> getParameter('dist');
        $data['detail_address'] = $request -> getParameter('detail').$data['address'];
        //如果是默认地址的话，就干掉原来的默认地址
        if( $data['default_type'] == 1 ){
            $this -> setNotDefault($this -> member_id);
        }
        if($this -> do_update($data,$request-> getParameter('id'))){
            AjaxResult::ajaxSuccessResult();
        }else{
            AjaxResult::ajaxFailtureResult();
        }
    }

    /**
     * 删除地址，直接硬删除物理数据
     * @param HttpRequest $request
     * @return mixed
     */
    public function do_delete(HttpRequest $request)
    {
        $id = $request -> getParameter('id');
        $conditions['id'] = $id;
        $conditions['member_id'] = $this -> member_id;
        $service = Beans::get( $this -> getServiceBean() );
        if( $service -> deletes($conditions) !==false ){
            AjaxResult::ajaxSuccessResult();
        }
        AjaxResult::ajaxSuccessResult();
    }

    /**
     * 设置默认地址
     * @param HttpRequest $request
     */
    public function setDefault(HttpRequest $request)
    {
        if( strtolower($_SERVER['REQUEST_METHOD']) !="post"  ){
            echo "<h1>Access Deny</h1>";die;
        }
        $data['default_type']=1;
        $id = $request -> getParameter('id');
        $this -> setNotDefault($this -> member_id);
        if( $this -> do_update($data,$id) ){
            AjaxResult::ajaxSuccessResult();
        }else{
            AjaxResult::ajaxFailtureResult();
        }
        //$this -> index($request);
    }

    /**
     * 更新数据
     * @param array $data
     * @param int $id
     * @return bool
     */
    protected function do_update( $data, $id ){
        $conditions['id'] = $id;
        $conditions['member_id'] = $this -> member_id;
        $service = Beans::get( $this -> getServiceBean() );
        if( $service -> updates($data,$conditions) !==false ){
            return true;
        }
        return false;
    }

    /**
     * 干掉默认地址
     * @param $member_id
     * @return bool
     */
    protected function setNotDefault( $member_id ){
        $data['default_type']=0;
        $conditions['member_id'] = $member_id;
        $service = Beans::get( $this -> getServiceBean() );
        if( $service -> updates($data,$conditions) !==false ){
            return true;
        }
        return false;
    }

}